package com.size.mall.mallsearch.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.size.mall.mallapi.entity.PmsSearchParam;
import com.size.mall.mallapi.entity.PmsSearchSkuInfo;
import com.size.mall.mallapi.service.SearchService;
import io.searchbox.client.JestClient;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@Service
public class SearchServiceImpl implements SearchService {

    @Autowired
    JestClient jestClient;


    @Override
    public List<PmsSearchSkuInfo> ls(PmsSearchParam param) {


        // jest的 dsl 工具
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        // bool
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();

        // filter条件
        String[] valueIds = param.getValueId();

        if (ArrayUtil.isNotEmpty(valueIds)) {

            for (String valueId : valueIds) {

                TermQueryBuilder termQueryBuilder = new TermQueryBuilder("pmsSkuAttrValueList.valueId", valueId);
                boolQueryBuilder.filter(termQueryBuilder);
            }


        }

        String catalog3Id = param.getCatalog3Id();

        if (StrUtil.isNotBlank(catalog3Id)) {

            TermQueryBuilder termQueryBuilder = new TermQueryBuilder("catalog3Id", catalog3Id);
            boolQueryBuilder.filter(termQueryBuilder);

        }


        String keyword = param.getKeyword();


        if (StrUtil.isNotBlank(keyword)) {


            MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("skuName", keyword);
            boolQueryBuilder.must(matchQueryBuilder);

        }


        //设置条件
        searchSourceBuilder.query(boolQueryBuilder);


        // 分页和高亮
        searchSourceBuilder.from(0);

        searchSourceBuilder.size(20);


        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.preTags("<span style='color:red;'>");

        highlightBuilder.field("skuName");
        highlightBuilder.postTags("</span>");

        searchSourceBuilder.highlight(highlightBuilder);


        searchSourceBuilder.sort("id", SortOrder.DESC);

        String query = searchSourceBuilder.toString();


        System.out.println(query);

        /**
         *  以下 查询与封装
         */
        Search search = new Search.Builder(query).addIndex("mall").addType("searchSku").build();

        List<SearchResult.Hit<PmsSearchSkuInfo, Void>> hits = null;

        // jest查询
        try {
            SearchResult execute = jestClient.execute(search);
            hits = execute.getHits(PmsSearchSkuInfo.class);

        } catch (IOException e) {
            e.printStackTrace();
        }
        List<PmsSearchSkuInfo> skus = CollUtil.newArrayList();
        for (SearchResult.Hit<PmsSearchSkuInfo, Void> hit : hits) {

            PmsSearchSkuInfo source = hit.source;
            //重置高亮字段
            Map<String, List<String>> highlight = hit.highlight;
            if (MapUtil.isNotEmpty(highlight)) {
                String skuName = highlight.get("skuName").get(0);
                source.setSkuName(skuName);
            }

            skus.add(source);

        }


        return skus;
    }
}
